package com.sky.task;

import com.sky.constant.MessageConstant;
import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Component
@Slf4j
public class OrderTask {
    @Autowired
    OrderMapper orderMapper;
    /**
     * 处理超时未支付的订单  每分钟执行这个检查方法 是否有超过15分钟没有支付
     */
    @Scheduled(cron = "0 * * * * *")
    //@Scheduled(cron = "* 1/5 * * * *")
    public void processOverTimeNoPaid(){
        log.info("定时处理超时未支付的订单：",LocalDateTime.now());
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime time=now.plusMinutes(-15);
        List<Orders> list=orderMapper.listOverTimeNoPaid(Orders.PENDING_PAYMENT,time);
        if(list!=null&&list.size()>0){
            for(Orders order:list){
                order.setStatus(Orders.CANCELLED);
                order.setCancelReason("订单超时，已取消");
                order.setCancelTime(LocalDateTime.now());
                orderMapper.update(order);
            }
        }
    }
    @Scheduled(cron = "0 0 1 * * *")
    //@Scheduled(cron = "* 1/5 * * * *")
    public void processAlwaysOnDelivery(){
        log.info("处理一直处于派送中的订单",LocalDateTime.now());
        List<Orders> list=orderMapper.listAlwaysOnDelivery(Orders.DELIVERY_IN_PROGRESS,LocalDateTime.now().plusMinutes(-60));
        if(list!=null&&list.size()>0){
            for(Orders order:list){
                order.setStatus(Orders.COMPLETED);
                orderMapper.update(order);
            }
        }

    }
}
